package com.transpot.service.impl.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.transpot.entry.exception.Exception;
import com.transpot.entry.money.Stream;
import com.transpot.entry.order.Order;
import com.transpot.entry.order.OrderFee;
import com.transpot.entry.order.OrderReturn;
import com.transpot.mapper.exception.ExceptionMapper;
import com.transpot.mapper.money.StreamMapper;
import com.transpot.mapper.order.OrderFeeMapper;
import com.transpot.mapper.order.OrderMapper;
import com.transpot.mapper.order.OrderReturnMapper;
import com.transpot.service.order.OrderReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 订单回执单类的service层实现类
 */
@Service
public class OrderReturnServiceImpl extends ServiceImpl<OrderReturnMapper, OrderReturn> implements OrderReturnService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderReturnMapper orderReturnMapper;
    @Autowired
    private ExceptionMapper exceptionMapper;
    @Autowired
    private OrderFeeMapper orderFeeMapper;
    @Autowired
    private StreamMapper streamMapper;
    /**
     * 完成订单方法实现
     * @param orderReturn
     */
    @Override
    public void finishOrder(OrderReturn orderReturn) {
        //设置回执单状态为已处理
        orderReturn.setStatus(1);
        //调用方法修改
        orderReturnMapper.updateById(orderReturn);
        //创建订单整合类条件查询对象
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件为订单基本信息主键
        orderLambdaQueryWrapper.eq(Order::getBaseId, orderReturn.getBaseId());
        //调用mp的方法获取订单基本信息
        Order order = orderMapper.selectOne(orderLambdaQueryWrapper);
        //设置订单状态为已完成
        order.setStatus(3);
        //修改订单整合类信息
        orderMapper.update(order, orderLambdaQueryWrapper);
        //创建异常信息条件对象
        LambdaQueryWrapper<Exception> exceptionLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件为与该订单基本信息相同
        exceptionLambdaQueryWrapper.eq(Exception::getBaseId, order.getBaseId());
        //删除异常信息
        exceptionMapper.delete(exceptionLambdaQueryWrapper);

        //设置该订单的流水
        //创建流水对象
        Stream stream = new Stream();
        //查询订单费用信息
        OrderFee orderFee = orderFeeMapper.selectById(order.getFeeId());
        //判断费用组成
        //如果到付，回付，周期付都为0
        if (orderFee.getCarriageForward() == 0 &&
                orderFee.getBackPayment() == 0 && orderFee.getPeriodicPayment() == 0) {
            //则费用组成为预付款加先付款
            stream.setMoney(orderFee.getAdvance() + orderFee.getCashPayment());
            //流水类型为收入
            stream.setType(0);
            //设置说明
            stream.setMark("运输订单完成结算收入");
            //设置创建时间
            stream.setCreateTime(LocalDateTime.now());
            //设置订单主键
            stream.setBaseId(order.getBaseId());
            //信息插入数据库
            streamMapper.insert(stream);
        }
        //如果到付不为0，回付，周期付都为0
        if (orderFee.getCarriageForward() != 0
                && orderFee.getBackPayment() == 0 && orderFee.getPeriodicPayment() == 0) {
            //如果回执单中到付款大于等于应该的到付款
            if (orderReturn.getPay() >= orderFee.getCarriageForward()) {
                //收入为预付加现付加到付
                stream.setMoney(orderFee.getAdvance() + orderFee.getCashPayment() + orderReturn.getPay());
                //流水类型为收入
                stream.setType(0);
                //设置说明
                stream.setMark("运输订单完成结算收入");
                //设置创建时间
                stream.setCreateTime(LocalDateTime.now());
                //设置订单主键
                stream.setBaseId(order.getBaseId());
                //信息插入数据库
                streamMapper.insert(stream);
            } else { //到付小于应该的到付金额
                //计算收入
                //收入为预付加现付加到付
                stream.setMoney(orderFee.getAdvance() + orderFee.getCashPayment() + orderReturn.getPay());
                //流水类型为收入
                stream.setType(0);
                //设置说明
                stream.setMark("运输订单完成结算收入，存在欠款");
                //设置创建时间
                stream.setCreateTime(LocalDateTime.now());
                //设置订单主键
                stream.setBaseId(order.getBaseId());
                //信息插入数据库
                streamMapper.insert(stream);

                //计算欠款
                //欠款为应该到付减去实际到付
                Stream stream1 = new Stream();
                stream1.setMoney(orderFee.getCarriageForward() - orderReturn.getPay());
                //设置说明
                stream1.setMark("到付款未全部付清");
                //设置订单主键
                stream1.setBaseId(order.getBaseId());
                //设置创建时间
                stream1.setCreateTime(LocalDateTime.now());
                //设置流水类型为欠款
                stream1.setType(2);
                //信息插入数据库
                streamMapper.insert(stream1);
            }
        }
        //如果到付为0，回付不为0，周期付为0
        if (orderFee.getCarriageForward() == 0
                && orderFee.getBackPayment() != 0 && orderFee.getPeriodicPayment() == 0) {
            //当预付和现付不为0时，被记作收入
            if (orderFee.getAdvance() + orderFee.getCashPayment() != 0) {
                stream.setMoney(orderFee.getAdvance() + orderFee.getCashPayment());
                //流水类型为收入
                stream.setType(0);
                //设置说明
                stream.setMark("运输订单预付款和先付款收入，待回付款");
                //设置创建时间
                stream.setCreateTime(LocalDateTime.now());
                //设置订单主键
                stream.setBaseId(order.getBaseId());
                //信息插入数据库
                streamMapper.insert(stream);
            }
            Stream stream1 = new Stream();
            //回付记作欠款
            stream1.setMoney(orderFee.getBackPayment());
            //流水类型为欠款
            stream1.setType(2);
            //设置说明
            stream1.setMark("该订单需要收取回付款");
            //设置创建时间
            stream1.setCreateTime(LocalDateTime.now());
            //设置订单主键
            stream1.setBaseId(order.getBaseId());
            //信息插入数据库
            streamMapper.insert(stream1);
        }
        //如果到付为0，回付为0，周期付不为0
        if (orderFee.getCarriageForward() == 0
                && orderFee.getBackPayment() == 0 && orderFee.getPeriodicPayment() != 0) {
            //当预付和现付不为0时，被记作收入
            if (orderFee.getAdvance() + orderFee.getCashPayment() != 0) {
                stream.setMoney(orderFee.getAdvance() + orderFee.getCashPayment());
                //流水类型为收入
                stream.setType(0);
                //设置说明
                stream.setMark("运输订单完成结算收入");
                //设置创建时间
                stream.setCreateTime(LocalDateTime.now());
                //设置订单主键
                stream.setBaseId(order.getBaseId());
                //信息插入数据库
                streamMapper.insert(stream);
            }
            Stream stream1 = new Stream();
            //计算总付款为全部费用减去预付款和先付款，则剩余金额为周期付款欠款
            double total = orderFee.getTransportation() + orderFee.getPremium() + orderFee.getCod()
                    + orderFee.getCollectionFee() + orderFee.getPacking() + orderFee.getUpstairsFee()
                    + orderFee.getRentalFee() + orderFee.getDischarge() + orderFee.getPickChange()
                    + orderFee.getInterchangeFee() + orderFee.getDeliveryFee() + orderFee.getGodownChange()
                    + orderFee.getFuelSurcharge() + orderFee.getOtherCharge() - orderFee.getAdvance()
                    - orderFee.getCashPayment();

            //周期付总费用记作欠款
            stream1.setMoney(total);
            //流水类型为欠款
            stream1.setType(2);
            //设置说明
            stream1.setMark("该订单需要周期付款");
            //设置创建时间
            stream1.setCreateTime(LocalDateTime.now());
            //设置订单主键
            stream1.setBaseId(order.getBaseId());
            //信息插入数据库
            streamMapper.insert(stream1);
        }
    }
}
